package Aop.Utility;

import Aop.Element.Vector;
import Aop.Variable.AopStatic;
import android.graphics.Rect;
import android.graphics.RectF;

public class CollisionUtility
{
	public CollisionUtility()
	{
		throw new Error("Static Class");
	}
	
	static public boolean collisionDotBox(Vector position, Vector pposition, Vector aposition)
	{
		if(position.GetX() - aposition.GetX() <= pposition.GetX() && position.GetX() + aposition.GetX() >= pposition.GetX())
		{
			if( position.GetY() - aposition.GetY() <= pposition.GetY() && position.GetY() + aposition.GetY() >= pposition.GetY())
			{
				return true;
			}
		}
		return false;
	}
	
	static public boolean collisionDotBox(Rect $rect, Vector $vector)
	{
		boolean result = false;
		if($rect.left <= $vector.GetX() && $rect.right >= $vector.GetX()) {
			if($rect.top <= $vector.GetY() && $rect.bottom >= $vector.GetY()) {
				result = true;
			}
		}
		
		return result;
	}
	
	static public boolean collisionDotBox(RectF $rect, Vector $vector)
	{
		boolean result = false;
		if($rect.left <= $vector.GetX() && $rect.right >= $vector.GetX()) {
			if($rect.top <= $vector.GetY() && $rect.bottom >= $vector.GetY()) {
				result = true;
			}
		}
		
		return result;
	}
	
	static public boolean collisionDotBox(Vector position, Vector pposition, Vector aposition, String horizon, String vertical)
	{
		if(horizon == AopStatic.HORIZONTAL_LEFT)
		{
			if(position.GetX() >= pposition.GetX() && position.GetX() <= pposition.GetX() + aposition.GetX())
			{
				if(vertical == AopStatic.VERTICAL_TOP)
				{
					if(position.GetY() >= pposition.GetY() && position.GetY() <= pposition.GetY() + aposition.GetY() )
						return true;
				}
				else if(vertical == AopStatic.VERTICAL_BOTTOM)
				{
					if(position.GetY() >= pposition.GetY() - aposition.GetY() && position.GetY() <= pposition.GetY())
						return true;
				}
				else
				{
					if(position.GetY() - (aposition.GetY() / 2) <= pposition.GetY() && position.GetY() + (aposition.GetY() / 2) >= pposition.GetY())
						return true;
				}
			}
		}
		else if(horizon == AopStatic.HORIZONTAL_RIGHT)
		{
			if(position.GetX() >= pposition.GetX() - aposition.GetX() && position.GetX() <= pposition.GetX())
			{
				if(vertical == AopStatic.VERTICAL_TOP)
				{
					if(position.GetY() >= pposition.GetY() && position.GetY() <= pposition.GetY() + aposition.GetY() )
						return true;
				}
				else if(vertical == AopStatic.VERTICAL_BOTTOM)
				{
					if(position.GetY() >= pposition.GetY() - aposition.GetY() && position.GetY() <= pposition.GetY())
						return true;
				}
				else
				{
					if(position.GetY() - (aposition.GetY() / 2) <= pposition.GetY() && position.GetY() + (aposition.GetY() / 2) >= pposition.GetY())
						return true;
				}
			}
		}
		else
		{
			if(position.GetX() - (aposition.GetX() / 2) <= pposition.GetX() && position.GetX() + (aposition.GetX() / 2) >= pposition.GetX())
			{
				if(vertical == AopStatic.VERTICAL_TOP)
				{
					if(position.GetY() >= pposition.GetY() && position.GetY() + aposition.GetY() <= pposition.GetY())
						return true;
				}
				else if(vertical == AopStatic.VERTICAL_BOTTOM)
				{
					if(position.GetY() - aposition.GetY() >= pposition.GetY() && position.GetY() <= pposition.GetY())
						return true;
				}
				else
				{
					if(position.GetY() - (aposition.GetY() / 2) <= pposition.GetY() && position.GetY() + (aposition.GetY() / 2) >= pposition.GetY())
						return true;
				}
			}
		}
		return false;
	}
	
	static public boolean collisionBoxBox(Vector position, Vector pposition, Vector aposition, Vector bposition)
	{
		if((position.GetX() - aposition.GetX() >= pposition.GetX() - bposition.GetX() && position.GetX() - aposition.GetX() <= pposition.GetX() + bposition.GetX()) 
				|| (position.GetX() - aposition.GetX() <= pposition.GetX() - bposition.GetX() && position.GetX() + aposition.GetX() >= pposition.GetX() - bposition.GetX()))
		{
			if(position.GetY() - aposition.GetY() >= pposition.GetY() - bposition.GetY() && position.GetY() - aposition.GetY() <= pposition.GetY() + bposition.GetY())
			{
				return true;
			}
			if(position.GetY() - aposition.GetY() <= pposition.GetY() - bposition.GetY() && position.GetY() + aposition.GetY() >= pposition.GetY() - bposition.GetY())
			{
				return true;
			}
		}
		return false;
	}

	static public boolean checkBoxCircle(Vector position, Vector pposition, Vector aposition, Vector bposition, int detail)
	{
		float angleDivide = 360 / detail;
		for(int roll = 0; roll < detail; roll++)
		{
			//angleNow = roll * angleEx;
			float tempX = (float)(position.GetX() + (float)(Math.cos(Math.toRadians(roll * angleDivide)) * (aposition.GetX())));
			float tempY = (float)(position.GetY() + (float)(Math.sin(Math.toRadians(roll * angleDivide)) * (aposition.GetY())));

			if(collisionDotBox(new Vector(tempX, tempY), pposition, bposition))
			{
				return true;
			}
		}
		
		return false;
	}
	
	static public boolean checkRange(Vector position, Vector pposition, float length)
	{
		double result = 0;
		result = position.Distance(pposition);
		
		if(result < length)
		{
			return true;
		}
		return false;
	}
}
